package school

import (
	"sort"
)

const testVersion = 1

type Grade struct {
	g      int
	roster []string
}

type School struct {
	grade []Grade
}

func (s School) Len() int {
	return len(s.grade)
}

func (s School) Swap(i, j int) {
	s.grade[i], s.grade[j] = s.grade[j], s.grade[i]
}

func (s School) Less(i, j int) bool {
	return s.grade[i].g < s.grade[j].g
}

func New() *School {
	s := School{}
	s.grade = make([]Grade, 0)
	return &s
}

func (s *School) Add(name string, input int) {

	existGrade := false
	for i := 0; i < len(s.grade); i++ {
		if s.grade[i].g == input {
			s.grade[i].roster = append(s.grade[i].roster, name)
			sort.Strings(s.grade[i].roster)
			existGrade = true
			break
		}
	}

	if !existGrade {
		grage := Grade{input, []string{name}}
		s.grade = append(s.grade, grage)
	}
}
func (s *School) Grade(input int) []string {
	for i := 0; i < len(s.grade); i++ {
		if s.grade[i].g == input {
			return s.grade[i].roster
		}
	}

	return nil
}
func (s *School) Enrollment() []Grade {
	// for i := 0; i < len(s.grade); i++ {
	// 	sort.Strings(s.grade[i].roster)
	// }
	sort.Sort(s)
	return s.grade
}
